using System;
using System.Windows.Forms;

namespace ClientTools.Client.Controls.Media.Dialogs
{
    public partial class AddImageDialog : Form
    {
        public AddImageDialog()
        {
            InitializeComponent();
        }

        private void OkButtonClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void AddImageClick(object sender, EventArgs e)
        {
            if (OpenFileDialog1.ShowDialog(this) == DialogResult.OK)
                foreach (string s in OpenFileDialog1.FileNames)
                    if (!IsAllreadyInList(s))
                        lbxImages.Items.Add(s);
        }

        private bool IsAllreadyInList(string s)
        {
            foreach (string li in lbxImages.Items)
                if (li == s)
                    return true;
            return false;
        }

        private void RemoveSelectedItemsClick(object sender, EventArgs e)
        {
            var s = new string[lbxImages.SelectedItems.Count - 1];

            for (var i = 0; i < lbxImages.SelectedItems.Count; i++)
                s[i] = (string)lbxImages.SelectedItems[i];
            
            for (var i = 0; i < s.Length; i++)
                lbxImages.Items.Remove(s[i]);
        }

        private void AddImageDialog_Load(object sender, EventArgs e)
        {
            Label1.Text = "Select the image(s)";
        }

        public int MaxImageSize 
        { 
            get { return nudMaxSize.Value == 0 ? 800 : (int) nudMaxSize.Value; }
        }
    }
}